<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SignUpJobPosition;
use App\Models\CompanyModel;
use App\Models\JobpositionModel;
use App\Models\User as UserModel;
use App\Services\Tenancy\Tenancy;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class SignUpJobPositionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SignUpJobPosition(['userInfo', 'company']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id');
            $grid->combine('用户信息', ['created_at', 'userInfo.name', 'userInfo.gender', 'age', 'userInfo.mobile']);
            $grid->combine('职位信息', ['company', 'position_name', 'contact_type', 'contact', 'contact_img']);


            $grid->column('created_at', '报名时间')->sortable();
            $grid->column('userInfo.name', '用户姓名');
            $grid->column('userInfo.gender', '性别')->using(UserModel::GENDER_LIST)->dot(
                [
                    1 => 'primary',
                    2 => 'pink',
                    3 => 'success',
                ],
            );
            $grid->column('age', '年龄')->display(function () {
                return $this->userInfo->age();
            });
            $grid->column('userInfo.mobile', '联系方式');
            $grid->column('position_name');
            $grid->column('company', '公司')->display(function ($value) {
                return $value->cur_type == CompanyModel::TYPE_PERSON ? $value->contact_person_name : $value->company_name;
            });
            $grid->column('contact_type')->using(JobpositionModel::CONTACT_TYPE_LIST);

            $grid->column('contact', '报名方式')->display(function () {
                $info = '';
                if ($this->contact_type == JobpositionModel::CONTACT_MOBILE) {
                    $info = $this->phone_number;
                }
                if ($this->contact_type == JobpositionModel::CONTACT_QQ) {
                    $info = "$this->qq_number";
                }
                if ($this->contact_type == JobpositionModel::CONTACT_WECHAT) {
                    $info = "$this->wechat_number";
                }
                if ($this->contact_type == JobpositionModel::CONTACT_DINGTALK) {
                    $info = "$this->dingtalk_number";
                }
                return $info;
            });
            $grid->column('contact_img', '二维码')
                ->display(function () {
                    $img = '';
                    if ($this->contact_type == JobpositionModel::CONTACT_QQ) {
                        $img = $this->qq_qr_code;
                    }
                    if ($this->contact_type == JobpositionModel::CONTACT_WECHAT) {
                        $img = $this->wechat_qr_code;
                    }
                    if ($this->contact_type == JobpositionModel::CONTACT_DINGTALK) {
                        $img = $this->dingtalk_qr_code;
                    }
                    if ($img) {
                        return $img;
                    }
                })->image('', 100, 100);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $jobPosition = JobpositionModel::query()->select('position_name', 'id')->get();
                $jobPositionList = [];
                foreach ($jobPosition as $item) {
                    $jobPositionList[$item->id] = $item->position_name;
                }
                $filter->equal('user_id', '用户ID')->width(2);
                $filter->like('userInfo.name', '用户姓名')->width(2);
                $filter->equal('userInfo.mobile', '联系方式')->width(2);
                $filter->equal('company_id', '公司')->select(Tenancy::selectLis())->width(2);
                $filter->equal('job_position_id', '职位')->select($jobPositionList)->width(2);
                $filter->between('created_at', '报名时间')->datetime()->width(4);
            });
            $titles = [];
            $filename = '';
            if (request('company_id') > 0) {
                $titles = [
                    'position_name' => '职位名称',
                    'user_name' => '用户姓名',
                    'user_mobile' => '联系方式',
                    'created_at' => '报名时间',
                ];
                $company = CompanyModel::query()->where('id', request('company_id'))->first();
                $company_name = $company->cur_type == CompanyModel::TYPE_PERSON ? $company->contact_person_name : $company->company_name;
                $filename = $company_name . time();
            }
            if (request('job_position_id') > 0) {
                $titles = [
                    'user_name' => '用户姓名',
                    'user_mobile' => '联系方式',
                    'created_at' => '报名时间',
                ];
                $position = JobpositionModel::query()->where('id', request('job_position_id'))->first();
                $company = CompanyModel::query()->where('id', $position->company_id)->first();
                $company_name = $company->cur_type == CompanyModel::TYPE_PERSON ? $company->contact_person_name : $company->company_name;
                $filename = "$company_name-{$position->position_name}" . time();
            }

            //            $titles = [
            //                'company_name' => '公司',
            //                'position_name' => '职位名称',
            //                'user_name' => '用户姓名',
            //                'user_gender' => '用户性别',
            //                'user_age' => '用户年龄',
            //                'user_mobile' => '联系方式',
            //                'user_identity' => '身份',
            //                'user_self_description' => '自我描述',
            //                'user_education' => '教育经历',
            //                'user_work_experience' => '工作经历',
            //                'created_at' => '报名时间',
            //            ];
            //            $grid->export(new ExcelExpoter());
            if (!empty(request('company_id')) || !empty(request('job_position_id'))) {
                $grid->export()->titles($titles)->rows(function ($rows) {
                    foreach ($rows as &$row) {
                        $row['user_name'] = $row['userInfo']['name'] ?? '';
                        $row['user_mobile'] = $row['userInfo']['mobile'] ?? '';
                    }
                    return $rows;
                })->filename($filename);
            }

            //            $grid->order->orderable(); // 开启排序功能
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            // 禁用批量删除按钮
            $grid->disableBatchDelete();
            $grid->disableActions();
            $grid->disableCreateButton();
            // 禁用显示按钮
            $grid->disableViewButton();
            $grid->disableRowSelector();
        });
    }
}
